package business.algorithm.technicalIndicator;

import dataAccess.databaseManagement.entity.AssetEntity;
import dataAccess.databaseManagement.entity.PriceEntity;
import java.util.ArrayList;
import java.util.TreeMap;
import utility.MathUtility;

public class SimpleMovingAverage extends AbstractTechnicalIndicator {
	
	private Integer period;
	
	public SimpleMovingAverage()
	{
		super(null);
		this.period = null;
	}
	
	public SimpleMovingAverage(
			TreeMap<AssetEntity, ArrayList<PriceEntity>> priceList, int period) {
		super(priceList);
		// TODO Auto-generated constructor stub
		this.period = period;
	}

	public int getPeriod() {
		return period;
	}

	public void setPeriod(int period) {
		this.period = period;
	}

	@Override
	public ArrayList<ArrayList<PointEntry>> runAlgorithm() {
		// TODO Auto-generated method stub
		ArrayList<PointEntry> resultSeries = new ArrayList<PointEntry>();
		for (AssetEntity asset : priceList.keySet())
		{
			ArrayList<PriceEntity> priceEntityList = priceList.get(asset);
			ArrayList<Double> series = new ArrayList<Double>();
			for (PriceEntity price : priceEntityList)
				series.add(price.getClose());
			ArrayList<Double> smaSeries = MathUtility.SMA(series, period);
			for (int i = 0; i < smaSeries.size(); ++i)
				resultSeries.add(new PointEntry(priceEntityList.get(i + period - 1).getDate(), smaSeries.get(i)));
			break;
		}
		ArrayList<ArrayList<PointEntry>> result = new ArrayList<ArrayList<PointEntry>>();
		result.add(resultSeries);
		return result;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public TreeMap<String, Class> getParameterList() {
		// TODO Auto-generated method stub
		TreeMap<String, Class> map = super.getParameterList();
		map.put("period", Integer.class);
		return map;
	}

	@Override
	public void setParameterValue(TreeMap<String, Object> map) {
		// TODO Auto-generated method stub
		super.setParameterValue(map);
		this.period = (Integer) map.get("period");
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Simple Moving Average - " + period;
	}

    @Override
    public int getType() {
        return AbstractTechnicalIndicator.PRICE_CHART;
    }
	
}
